package org.zfes.snowier.cloud.zbss.cms.content.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.content.service.ICmsContentColumnService;
import org.zfes.snowier.cloud.zbss.cms.content.dao.CmsContentColumnMapper;
import org.zfes.snowier.cloud.zbss.cms.content.model.CmsContentColumn;
import java.util.stream.Stream;
@Service
public class CmsContentColumnServiceImpl implements ICmsContentColumnService{
 	@Autowired
	private CmsContentColumnMapper cmsContentColumnMapper;
	//===============================增
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsContentColumn(CmsContentColumn cmsContentColumn) {
		ZBeanUtil.validateBean(cmsContentColumn);
		cmsContentColumn.setStatus(Byte.valueOf("1"));//状态:_D#1 启用 0禁用 -1删除#
		cmsContentColumn.setCreateTime(ZDateUtil.newDate());
		cmsContentColumn.setUpdateTime(ZDateUtil.newDate());
		cmsContentColumnMapper.insertSelective(cmsContentColumn);
	}
	
	//=============================删
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsContentColumn(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsContentColumnMapper.deleteByPrimaryKey(id);
	}
	//=============================查
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsContentColumn> loadCmsContentColumnById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsContentColumnMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsContentColumnDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsContentColumnMapper.selectListCount(pm), cmsContentColumnMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsContentColumn> loadCmsContentColumnByContentId(Long contentId) {
		if(contentId!=null){
			return Optional.ofNullable(cmsContentColumnMapper.selectCmsContentColumnByContentId(contentId));
		}
		return Optional.empty();
	}
	
	//=============================改
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsContentColumn(CmsContentColumn cmsContentColumn) {
		ZBeanUtil.validateBean(cmsContentColumn);
		cmsContentColumn.setUpdateTime(ZDateUtil.newDate());
		cmsContentColumnMapper.updateByPrimaryKeySelective(cmsContentColumn);
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsContentColumnStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsContentColumnMapper.updateStatus( id,  status) ;
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsContentColumnStatusByContentId(Long contentId, Byte status) {
		ZAssert.notNull(contentId, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsContentColumnMapper.updateStatusByContentId(contentId,status) ;
	}
	
}